package org.headquarter.search;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.headquarter.object.Node;
import org.headquarter.object.UnicId;

public class NodeSearcher {
    IndexWriter indexWriter;
    Searcher searcher;
    QueryParser parser;
    
    String indexFolder;
    public NodeSearcher(String indexFolder)  {
	this.indexFolder=  indexFolder;
	
    }



    public void index(Node node) throws IndexError {
	try {
	indexWriter = new IndexWriter(
		FSDirectory.open(new File(indexFolder)),
		new StandardAnalyzer(Version.LUCENE_30),
		IndexWriter.MaxFieldLength.LIMITED);
	Document doc = new Document();
	doc.add(new Field("uid",node.getUnicId().toString(), Field.Store.YES,Field.Index.ANALYZED));
	doc.add(new Field("tag",node.getTags().getString(), Field.Store.YES,Field.Index.ANALYZED));
	doc.add(new Field("group",node.getGroups().getString(), Field.Store.YES,Field.Index.ANALYZED));
	if(node.getString()!=null){
	    doc.add(new Field("text",node.getString(), Field.Store.YES,Field.Index.ANALYZED));
	}
	    indexWriter.addDocument(doc);
	    indexWriter.close();
	} catch (CorruptIndexException e) {
	    throw new IndexError(e);
	} catch (IOException e) {
	    throw new IndexError(e);
	}
    }

    public List<Node> findNodeByGroups(Set<Tag> groups){
	return null;
    }

    public Node findNodeByUnicIdAndGroups(UnicId unicId,Set<Tag> groups){
	return null;
    }

    public List<Node> findNodeByTagsAndGroups(Set<Tag> tags,Set<Tag> groups){
	return null;
    }

    public List<Node> findNode(String queryString) throws SearchError{
	try {
	searcher = new IndexSearcher(FSDirectory.open(new File(indexFolder)));
	parser = new QueryParser(Version.LUCENE_30,"text",new StandardAnalyzer(Version.LUCENE_30));
	Query query;
	
	    query = parser.parse(queryString);

	    TopDocs results = searcher.search(query,10);
	    System.out.println("[" + queryString+"] hit "+results.totalHits);
	    ScoreDoc[] hits = results.scoreDocs;	   
	    for (ScoreDoc hit : hits) {
		Document doc = searcher.doc(hit.doc);

		System.out.printf("   %s (%5.3f)\n",
			doc.get("text"),hit.score);
	    }
	    searcher.close();
	} catch (ParseException e) {
	    throw new SearchError(e);
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
return null;

    }



    public void shutdown()  throws IndexError {
	/*try {
	    
	    
	} catch (CorruptIndexException e) {
	    throw new IndexError(e);
	} catch (IOException e) {
	    throw new IndexError(e);
	}*/
    }


}
